package cn.edu.ecut.se2024.controller;

import cn.edu.ecut.se2024.exception.OperationFailedException;
import cn.edu.ecut.se2024.model.ShoppingCart;
import cn.edu.ecut.se2024.model.UserMenu;
import cn.edu.ecut.se2024.service.ShoppingCartService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ShoppingCartController extends AuthorizedController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 显示商品信息
     */
    @RequestMapping("/sys.toShoppingCart")
    public String queryMyCarts (HttpServletRequest request) throws OperationFailedException {
        UserMenu userMenu = (UserMenu) request.getSession().getAttribute(SYS_USER_MENU);
        List<ShoppingCart> shoppingCarts = shoppingCartService.showMyCarts(userMenu.getUser().getUserId());
        request.getSession().setAttribute("showCarts", shoppingCarts);
        return SYS_MAIN_PAGE;
    }

    /**
     * 查询商品信息
     */
    @PostMapping("/sys.queryMyCarts")
    public String queryMyCarts(HttpServletRequest request,
                                   @RequestParam @Validated String searchInput)throws OperationFailedException {
        UserMenu userMenu = (UserMenu) request.getSession().getAttribute(SYS_USER_MENU);
        List<ShoppingCart> shoppingCarts = shoppingCartService.queryMyCarts(userMenu.getUser().getUserId(), searchInput);
        request.getSession().setAttribute("carts", shoppingCarts);
        return SYS_MAIN_PAGE;
    }
}
